<?php
class Score_model extends MY_Model {
	public function __construct(){
		parent::__construct();	
		$this->setTableName('u_c_index_weight');
	}
	
	public function getScore($tid){
		$s_score = "SELECT AVG(s_t.score)*iw.score as score,s_t.tid FROM wq_u_m_s_t as s_t,wq_u_c_index_weight as iw WHERE iw.classid=1 AND s_t.tid={$tid} GROUP BY s_t.tid";
		$d_score = "SELECT AVG(d_t.score)*iw.score as score,d_t.tid FROM wq_u_m_d_t as d_t,wq_u_c_index_weight as iw WHERE iw.classid=2 AND d_t.tid={$tid} GROUP BY d_t.tid";
		$a_score = "SELECT SUM(a_c.score) as score,t_a.tid FROM wq_u_c_activity as a,wq_u_c_activity_cat as a_c,wq_u_m_t_a as t_a WHERE t_a.tid={$tid} AND t_a.aid=a.classid AND a.acid=a_c.classid GROUP BY t_a.tid";
		$sql = "SELECT s.score+d.score+a.score as score FROM ({$s_score}) as s,({$d_score}) as d,({$a_score}) as a";
		$reData = $this->db->query($sql)->row_array();
		return $reData['score'];
	}
	
	public function saveScore($table,$id,$score){
		$sql = "UPDATE {$table} SET score={$score} WHERE id=$id";
		$this->db->query($sql);
	}
	
	public function saveStudentScore($id,$score){
		$this->saveScore("wq_u_m_s_t",$id,$score);
	}
	
	public function saveDepartmentScore($tid,$score){
		$sql = "INSERT INTO wq_u_m_d_t(tid,score) VALUES ($tid,$score)";
		$this->db->query($sql);
	}
	
}
?>